home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / bplus25a.zip / BPLUS.DOC < prev    next >
Text File  |  1990-12-30  |  25KB  |  605 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.                                THE B-PLUS PROGRAM
  7.                    Version 2.5
  8.              A B-TREE INDEXING FILE MODULE
  9.                                FOR C PROGRAMMERS
  10.                                        by
  11.                              Hunter and Associates
  12.  
  13.  
  14.  
  15.               B-PLUS is a versatile, carefully designed module for C
  16.          programmers who need a fast, efficient program for indexing
  17.          data files.  B-PLUS allows data records to be retrieved based
  18.          on a key value without regard to their position in the data
  19.          file.  The data records can also be accessed in sequential
  20.          order in either a forward and reverse direction.
  21.  
  22.               The B-PLUS Program Module is based on the famous and
  23.          widely used b-tree algorithm and has a number of useful
  24.          extensions which are not found in many programs of this type.
  25.          Some of its features are the following:
  26.  
  27.               - Variable length keys are allowed
  28.  
  29.               - File size limited only by DOS or by disk space
  30.  
  31.               - All functions are non-recursive so very little stack
  32.                 space is required
  33.  
  34.               - The most recently used key values are stored in a
  35.                 cache buffer in main memory for fast access
  36.  
  37.               - Duplicate keys are allowed
  38.  
  39.           Version 2.5 of the B-PLUS program has been tested
  40.      for Microsoft C Compilers, Versions 5.1, 6.0 and the
  41.          Borland Turbo C Compiler Version 1.5 and 2.0.  The object
  42.      file is approximately 10K bytes in length for these compilers.
  43.          See the instructions at the end of this user's guide for a
  44.          special note regarding Microsoft's older C Version 4.0.
  45.  
  46.           Version 2.5 has several new features that were not in
  47.      earlier versions.  The next_key and prev_key routines can be
  48.      called immediately after adding or deleting an index key.  The
  49.      first_key and last_key functions have been changed to return
  50.      the first or last ENTRY value on return.  A buffer_flush function
  51.      has been added to save the internal buffers on the disk.
  52.  
  53.  
  54.          LICENSE AND REGISTRATION
  55.  
  56.               B-PLUS is distributed as a "share ware" program.  Please
  57.          help us get it known by giving unmodified copies of the
  58.  
  59.  
  60.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  61.          -------------------------------------------------------------
  62.  
  63.  
  64.          program and documentation to other programmers who may find
  65.          B-PLUS useful.
  66.  
  67.           B-PLUS is copyright (C) by Hunter and Associates.
  68.          It is not public domain or free software.  Non-registered
  69.          users are granted a limited license to use B-PLUS on a trial
  70.          basis for determining whether or not it is suitable for their
  71.          needs.  Registration permits the use of B-PLUS on one CPU and
  72.          allows the use of the compiled B-PLUS modules in programs for
  73.          general sale and/or distribution.
  74.  
  75.           The B-Plus registration fee is $35.  Users who pay the
  76.      $35 fee will be sent a disk containing the complete
  77.      listings of the latest source code, the user documentation,
  78.      and a number of useful sample programs.  The registration
  79.      fee for the network version of B-Plus is $50.  Registered
  80.      users of B-Plus can order the network version for $25.
  81.  
  82.           Your prompt registration of your copy of the B-
  83.          PLUS program is appreciated.
  84.  
  85.               Register your usage of B-PLUS by sending the registra-
  86.          tion fee to:
  87.  
  88.                         Hunter and Associates
  89.                         7900 Edgewater Drive
  90.                         Wilsonville, OR  97070
  91.                         Telephone: (503) 694-1449
  92.  
  93.          Your comments regarding the B-PLUS program or any suggestions
  94.          you have for extensions or for other programs that would be
  95.          useful to you are welcomed.
  96.  
  97.               Hunter and Associates makes no warranties whatsoever
  98.          regarding the B-PLUS computer programs or the supporting
  99.          documentation.
  100.  
  101.  
  102.          USING B-PLUS IN YOUR PROGRAMS
  103.  
  104.               The B-PLUS File Indexing Module contains twelve
  105.          functions that handle the retrieval of data records by key
  106.          value.  The keys that are used to locate records are null
  107.          terminated strings.  The data structures and constants that
  108.          are used are defined in the header file bplus.h.
  109.  
  110.               If the data record field that you want to use as a key
  111.          contains numeric data, you can use one of the library
  112.  
  113.                                    Page 2
  114.  
  115.  
  116.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  117.          -------------------------------------------------------------
  118.  
  119.  
  120.          conversion routines (fcvt, evct, sprintf) to convert the data
  121.          to string format.
  122.  
  123.               The connection between a key and its reference is
  124.          formalized as a structure of type ENTRY.  This structure
  125.          contains three elements:
  126.  
  127.          typedef struct
  128.            {
  129.              RECPOS   idxptr;         /* long pointer to next index
  130.                                          level                      */
  131.              RECPOS   recptr;         /* long pointer to the file
  132.                                          position of data record    */
  133.              char     key[MAXKEY];    /* with this key value        */
  134.            } ENTRY;
  135.  
  136.               The application program uses only the recptr and key[]
  137.          fields.  The idxptr is used and maintained by the B-PLUS
  138.          modules.
  139.  
  140.               A variable of type IX_DESC is declared for each open
  141.          index file.  See the header file bplus.h if you are
  142.          interested in the elements of this structure.  ENTRY and
  143.          IX_DESC are the only two data types that are normally used by
  144.          application programs.
  145.  
  146.               Here is a sample program stub which calls the open_index
  147.          and find_index subroutines.
  148.  
  149.  
  150.          Example:
  151.  
  152.            #include "bplus.h"
  153.            main()
  154.              {
  155.                 ENTRY    e;
  156.                 IX_DESC  names;
  157.  
  158.                 /* open index file called NAMES.IDX */
  159.  
  160.                 open_index("NAMES.IDX", &names, 0);
  161.  
  162.                 /* find an index record for John Smith */
  163.  
  164.                 strcpy(e.key, "SMITH JOHN");
  165.                 if(find_key(&e, &names))
  166.                   printf("Data record address is %ld", e.recptr);
  167.                 else
  168.                   printf("Cannot find record for that key");
  169.               }
  170.  
  171.                                    Page 3
  172.  
  173.  
  174.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  175.          -------------------------------------------------------------
  176.  
  177.  
  178.          Each of the twelve subroutines is now described.
  179.  
  180.          int cdecl open_index(name, pix, dup);
  181.  
  182.               char *name;         File path name
  183.               IX_DESC *pix;       Pointer to index file variable
  184.               int dup;            0 - no duplicate keys allowed
  185.                                   1 - allow duplicate keys
  186.  
  187.               Description:  The open_index function is used to open
  188.               and initialize an existing index file specified by name
  189.               and prepares the file for subsequent reading or writing.
  190.               The file structure variable pix is defined in the
  191.               application program.  Duplicate keys are allowed or not
  192.               allowed depending on whether dup has the value of 0 or
  193.               1.  The open_index function returns the value IX_OK (1)
  194.               if the file is opened successfully.  If the file cannot
  195.               be opened, an error message is displayed and the program
  196.               is aborted.
  197.  
  198.  
  199.  
  200.          int cdecl make_index(name, pix, dup);
  201.  
  202.               char *name;         File path name
  203.               IX_DESC *pix;       Pointer to index file variable
  204.               int dup;            0 - no duplicate keys allowed
  205.                                   1 - allow duplicate keys
  206.  
  207.               Description:  The make_index function is used to create
  208.               and initialize a new index file specified by name and to
  209.               prepare the file for subsequent reading or writing.  If
  210.               a file of this name already exists, its contents are
  211.               destroyed when the new file is created.  The file
  212.               structure variable pix is defined in the application
  213.               program.  Duplicate keys are allowed or not allowed
  214.               depending on whether dup has the value of 0 or 1.  The
  215.               make_index function returns the value IX_OK (1) if the
  216.               file is created successfully.  If the file cannot be
  217.               created, an error message is displayed and the program
  218.               is aborted.
  219.  
  220.  
  221.  
  222.          int cdecl close_index(pix);
  223.  
  224.               IX_DESC *pix;       Pointer to index file variable
  225.  
  226.               Description:  The close_index file function clears the
  227.               internal cache buffer and closes the specified index
  228.  
  229.                                    Page 4
  230.  
  231.  
  232.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  233.          -------------------------------------------------------------
  234.  
  235.  
  236.               file.  It is very important that each index file be
  237.               closed.  Otherwise data that is stored in the internal
  238.  
  239.               cache buffer may be lost and the index file may not be
  240.               properly updated.  The close_index function returns the
  241.               value IX_OK (1) if the file is successfully closed.
  242.  
  243.  
  244.  
  245.          int cdecl find_key(pe, pix);
  246.  
  247.               ENTRY *pe;          Pointer to variable of type ENTRY
  248.               IX_DESC *pix;       Pointer to index file variable
  249.  
  250.               Description:  The find_key function searches the index
  251.               file for the key value contained in pe.key.  If an exact
  252.               match is found, the value IX_OK (1) is returned and the
  253.               location of the data record with this key value is
  254.               stored in pe.recptr.  If an exact match is not found,
  255.               the value IX_FAIL (0) is returned and pe.recptr is
  256.               undefined.  If the index file contains duplicate keys,
  257.               the first key is always found.
  258.  
  259.  
  260.  
  261.          int cdecl locate_key(pe, pix);
  262.  
  263.               ENTRY *pe;          Pointer to variable of type ENTRY
  264.               IX_DESC *pix;       Pointer to index file variable
  265.  
  266.               Description:  The locate key function searches the index
  267.               file for the first key value which is equal to or
  268.               greater than that stored in pe.key.  The location of the
  269.               data record which is equal to or greater than pe.key is
  270.               stored in pe.recptr.  This function can be used to
  271.               locate an entry in the index file when only part of the
  272.               key value is known.  If the index file contains
  273.               duplicate keys, locate_key will locate the first key.
  274.               The following values are returned by locate_key:
  275.  
  276.                    IX_OK  -  the value (1) is returned if an exact
  277.                              match is found
  278.  
  279.                    IX_FAIL - the value (0) is returned if an exact
  280.                              match is not found
  281.  
  282.                    EOIX  -   the value (-2) is returned for end of
  283.                              index if the search key is greater than
  284.                              all keys in the index file and pe.recptr
  285.                              is undefined.
  286.  
  287.                                    Page 5
  288.  
  289.  
  290.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  291.          -------------------------------------------------------------
  292.  
  293.  
  294.  
  295.  
  296.          int cdecl add_key(pe, pix);
  297.  
  298.               ENTRY *pe;          Pointer to variable of type ENTRY
  299.               IX_DESC *pix;       Pointer to index file variable
  300.  
  301.               Description:  The add_key function adds new entries to
  302.               the index file.  The calling program stores the key
  303.               value in pe.key and the data record address in
  304.               pe.recptr.  Add_key first looks to see if an entry with
  305.               this key already exists in the index file.  If no entry
  306.               with this key exists, the new entry is added.  If an
  307.               entry with this key already exists, the new entry is
  308.               added only if duplicate keys are allowed (as defined by
  309.               the open_index function).  If the entry is successfully
  310.               added, IX_OK (1) is returned; otherwise IX_FAIL (0) is
  311.               returned.
  312.  
  313.  
  314.  
  315.          int cdecl delete_key(pe, pix);
  316.  
  317.               ENTRY *pe;          Pointer to variable of type ENTRY
  318.               IX_DESC *pix;       Pointer to index file variable
  319.  
  320.               Description:  The delete_key function deletes entries
  321.               in the index file.  The key to be deleted is stored in
  322.               pe.key.  If duplicate records are allowed, the
  323.               corresponding data record address must also be stored in
  324.               pe.recptr.  In this case, delete key needs the record
  325.               number to distinguish entries.  If there are not
  326.               duplicate entries, this field is ignored.  If the entry
  327.               is successfully deleted, IX_OK (1) is returned;
  328.               otherwise IX_FAIL (0) is returned.  The space that was
  329.               occupied by the entry is marked as free for reused by
  330.               B_PLUS.
  331.  
  332.  
  333.  
  334.      int cdecl first_key(pe, pix);
  335.  
  336.               ENTRY *pe;          Pointer to variable of type ENTRY
  337.               IX_DESC *pix;       Pointer to index file variable
  338.  
  339.           Description:  The first_key function is used to retrieve
  340.           the first key in an index file.  After being called, the
  341.           ENTRY pe contains the first key data.  The first_key
  342.           function returns the value IX_OK (1) if successful.
  343.  
  344.  
  345.                                    Page 6
  346.  
  347.  
  348.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  349.          -------------------------------------------------------------
  350.  
  351.      int cdecl last_key(pe, pix);
  352.  
  353.               ENTRY *pe;          Pointer to variable of type ENTRY
  354.               IX_DESC *pix;       Pointer to index file variable
  355.  
  356.           Description:  The last_key function is used to retrieve
  357.           the last key in an index file.  After being called, the
  358.           ENTRY pe contains the last key data.  The last_key
  359.           function returns the value IX_OK (1) if successful.
  360.  
  361.  
  362.  
  363.          int cdecl next_key(pe, pix);
  364.  
  365.               ENTRY *pe;          Pointer to variable of type ENTRY
  366.               IX_DESC *pix;       Pointer to index file variable
  367.  
  368.               Description:  The next_key function returns the next
  369.           entry in the index file.  For the network version of
  370.           Bplus, the ENTRY pe should be set before this function
  371.           is called.  The next key data is returned in pe.  Next key
  372.           can be used to find the desired data record when duplicate
  373.           keys are allowed.  Next_key is used to process files sequential.
  374.           Next_key returns the value IX_OK (1) if the next key is
  375.           present and the value EOIX (-2) when the file pointer is at
  376.           the end of the index file.  The following program processes
  377.               an indexed file sequentially:
  378.  
  379.               #include "bplus.h"
  380.               main()
  381.                 {
  382.                   ENTRY e;
  383.                   IX_DESC names;
  384.  
  385.           open_index("names.idx", &names, 1);
  386.  
  387.                   /* now process the file sequentially */
  388.           first_key(&e, &names);
  389.           /* the first key data is in ENTRY e */
  390.                   ret = next_key(&e, &names);
  391.                   while (ret == IX_OK)
  392.                     {
  393.                       /* the data record location is e.recptr */
  394.                       /* the program would retrieve and process it */
  395.                       ret = next_key(&e, &names);
  396.                     }
  397.  
  398.                   /* remember to always close open files */
  399.                   close_index(&names);
  400.                 }
  401.  
  402.                                    Page 7
  403.  
  404.  
  405.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  406.          -------------------------------------------------------------
  407.  
  408.  
  409.          int cdecl prev_key(pe, pix);
  410.  
  411.               ENTRY *pe;          Pointer to variable of type ENTRY
  412.               IX_DESC *pix;       Pointer to index file variable
  413.  
  414.               Description:  The prev_key function returns the previous
  415.               entry in the index file.  For the network version of
  416.           Bplus, the ENTRY pe should be set before this function
  417.           is called.  The next key data is returned in pe. Prev_key
  418.           can be used to process files sequentially in reverse order.
  419.           Prev_key returns the value IX_OK (1) if there is a previous
  420.           key and the value EOIX (-2) when the file pointer is at the
  421.               beginning of the index file.
  422.  
  423.  
  424.  
  425.          int cdecl find_exact(pe, pix);
  426.  
  427.               ENTRY *pe;          Pointer to variable of type ENTRY
  428.               IX_DESC *pix;       Pointer to index file variable
  429.  
  430.               Description:  The find_exact function searches the index
  431.               file for the key value contained in pe.key and the data
  432.               record position stored in pe.recptr.  If an exact match
  433.               is found for both of these values, the value IX_OK (1)
  434.               is returned, and the internal index file pointer is set
  435.               to that position in the index file.  If an exact match
  436.               is not found, the value IX_FAIL (0) is returned.
  437.  
  438.  
  439.  
  440.          int cdecl buffer_flush(pix);
  441.  
  442.               IX_DESC * pix;     Pointer to index file variable
  443.               
  444.               Description:  The buffer_flush function writes any of
  445.               the internal working buffers that have data which has
  446.               been change to the disk.  This function can be called
  447.               after adding or deleting keys to insure that the disk
  448.               files are immediately updated. 
  449.  
  450.               
  451.          TAILORING OR CHANGING B-PLUS
  452.  
  453.               Most applications can use the B-PLUS program as it is
  454.          distributed by Hunter and Associates without any changes.  It
  455.          allows multiple, large data files to be indexed in a fast,
  456.          efficient manner.  However, a description of the values that
  457.          can be changed to tailor B-PLUS are given in this section.
  458.  
  459.               An index tree becomes full when no more entries can be
  460.          added to the tree.  This is the case when adding another
  461.  
  462.                                    Page 8
  463.  
  464.  
  465.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  466.          -------------------------------------------------------------
  467.  
  468.  
  469.          entry to the tree would cause the tree to grow larger than
  470.          its maximum allowed height.  This height depends on the size
  471.          of the index blocks and the average size of the keys used by
  472.          the data files.  The minimum capacity of a b-tree index is
  473.          given by the following formula:
  474.  
  475.               C = (B / E + 1) * (B / (2 * E)  + 1) ** (H - 1)
  476.  
  477.          where C is the number of entries in the index file, B is the
  478.          block size in bytes, E is the average size of an ENTRY in
  479.          bytes, and H is the maximum tree height.
  480.  
  481.               The maximum key size is defined by MAXKEY and is set at
  482.          100.  The block size is 1024 bytes as defined by IXB_SIZE.
  483.          Ten bytes are used by pointers so 1014 bytes are used by
  484.          entries.  The size of an ENTRY is 9 + the key length.
  485.  
  486.               Thus, if the average key length is 11, an average ENTRY
  487.          is 20 bytes long and the minimum number of entries that can
  488.          be contained in a tree of height 4 is:
  489.  
  490.               C = (1014 / 20 + 1) * (1014 / 40 + 1) ** 3
  491.                 = 945,072
  492.  
  493.          If the average ENTRY is 40 bytes long, the minimum number of
  494.          entries that can be contained in a tree of height 4 is
  495.          67,384.  The corresponding values for a tree of height 3 are
  496.          35,896 and 4927, respectively.
  497.  
  498.               The maximum tree height is determined by MAX_LEVELS and
  499.          is set to eight.  Very little memory space is used by
  500.          allowing the maximum tree height to be this large.  B-PLUS
  501.          increases the height of the tree dynamically as required by
  502.          the number of records in the data file.
  503.  
  504.               If your application does not use long keys and your
  505.          files are not huge, IXB_SIZE can be changed to 512 bytes with
  506.          only a slight degradation in performance.
  507.  
  508.               The root of an open index file is always memory resident
  509.          as defined by the variable of type IX_DESC.  A dynamic pool
  510.          of cache buffers is used for other index blocks of the tree.
  511.          The number of blocks in the pool is defined by NUM_BUFS with
  512.          a default value of 8.  Each memory block is sizeof(IXB_SIZE)
  513.          + 8 bytes in length so approximately 8K of memory is used for
  514.          cache storage of index blocks.  If the number of index files
  515.          that are open simultaneously is larger than 4, you may want
  516.          to increase NUM_BUFS.  If the usage of memory is critical,
  517.          the number of buffers can be decreased.  However, NUM_BUFS
  518.          must be at least 2.  In general, the speed of file access can
  519.  
  520.  
  521.                                    Page 9
  522.  
  523.  
  524.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  525.          -------------------------------------------------------------
  526.  
  527.          be expected to improve if the number of buffers is increased
  528.          since more of the index file is memory resident.
  529.  
  530.               Because some indices are always memory resident, and
  531.          because the DOS Operating System requires it, it is very
  532.          important that all open index files be closed before an
  533.          application program terminates.
  534.  
  535.               As stated earlier, the B-PLUS program has been tested
  536.          using Microsoft's Optimizing C Compilers, Versions 4, 4.5 and
  537.          5.0, and Borland's Turbo C, Version 1.0.  However, standard K
  538.          & R programming guidelines are followed so B-PLUS should be
  539.          able to be used with other C Compilers with little
  540.          modification.  Since B-PLUS is non-recursive, the usage of
  541.          stack space does not change dynamically.  It is recommend
  542.          that the B-PLUS program be complied without stack checking.
  543.          For Microsoft C, the /Ox option can be used to maximize speed
  544.          and minimize code size.  For Turbo C, B-PLUS can be complied
  545.          with maximum optimization to minimize the object size and
  546.          improve performance.
  547.  
  548.  
  549.          ACKNOWLEDGMENTS AND REFERENCES
  550.  
  551.               The following reference materials were used and helpful
  552.          in writing the B-PLUS program:
  553.  
  554.               Wirth, Niklaus:
  555.                      Algorithms + Data Structures = Programs
  556.                      Prentice Hall (1976)
  557.  
  558.               Hunt, William James:
  559.                      The C Toolbox
  560.                      (Serious C Programming for the IBM PC)
  561.                      Addison-Wesley (1985)
  562.  
  563.  
  564.               Wirth's book is a standard reference source for binary
  565.          trees and data structures.  Hunt's C Toolbox contains useful
  566.          C programming concepts with carefully constructed programming
  567.          examples.
  568.  
  569.  
  570.          USING THE BPLUS ROUTINES
  571.  
  572.               The BPLUS.C routines must be compiled and the object
  573.          file (BPLUS.OBJ) loaded with programs that use the B_PLUS
  574.          toolkit.  Several sample C programs have been included which
  575.          illustrate how to use the BPLUS Toolkit.  These programs
  576.          should be compiled and run to make sure your copy of the
  577.          program is correct.
  578.  
  579.  
  580.                                   Page 10
  581.  
  582.  
  583.  
  584.          HUNTER AND ASSOCIATES            B-PLUS FILE INDEXING PROGRAM
  585.          -------------------------------------------------------------
  586.  
  587.  
  588.          A SPECIAL NOTE REGARDING MICROSOFT C 4.0 COMPILER
  589.  
  590.               The Microsoft C library routines are different for
  591.          Version 4.0 and for Version 5.0 and Quick C.  In particular,
  592.          the memmove routine must be changed to the memcpy routine in
  593.          Version 4.0.  A macro is included in BPLUS.C which makes this
  594.          substitution.  Remove the comments delimiters for this version.
  595.  
  596.  
  597.  
  598.       FOR USERS OF THE BPLUS NETWORK VERSION
  599.  
  600.            The structure of the BPlus binary tree can change
  601.       between function calls of your program.  For this reason
  602.       the ENTRY pe should be set before the functions next_key(pe, pix)
  603.       or prev_key(pe, pix) are called.  On successful return, the
  604.       next ENTRY after the calling value of pe is returned in pe.
  605.